package com.linwen.excise.leetcode;

/**
 * @ClassName _852山脉数组的峰顶索引
 * @Description TODO
 * @Author zero
 * @DATE 2024/9/22 1:54 PM
 * @Version 1.0
 */
public class _852山脉数组的峰顶索引 {
    public static void main(String[] args) {
        _852山脉数组的峰顶索引 inst = new _852山脉数组的峰顶索引();
        int[] arr = new int[]{0,2,1,0};
        // System.out.println(inst.peakIndexInMountainArray2(arr));
        System.out.println(inst.peakIndexInMountainArray(arr));
    }

    public int peakIndexInMountainArray2(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]>arr[i+1]){
                return i;
            }
        }
        return -1;
    }
    public int peakIndexInMountainArray(int[] arr) {
        int n = arr.length;
        int left = 1, right = n - 2, ans = 0;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] > arr[mid + 1]) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }


}
